

package top.continew.admin.question.user.service.question.factory;

import top.continew.admin.question.user.service.question.entity.*;
import top.continew.admin.question.user.service.question.enums.QuestionTypeEnum;
import top.continew.admin.question.model.question.Question;

public class QuestionFactory {

    public static AbstractQuestion createQuestion(Question question) {
        if (question == null) {
            throw new IllegalArgumentException("Question cannot be null");
        }
        Integer typeCode = Math.toIntExact(question.getType());
        QuestionTypeEnum type = QuestionTypeEnum.getEnumByCode(typeCode);
        if (type == null) {
            throw new IllegalArgumentException("Invalid question type code: " + typeCode);
        }
        AbstractQuestion questionInstance = createQuestionByType(type);
        questionInstance.init(question);
        return questionInstance;
    }

    private static AbstractQuestion createQuestionByType(QuestionTypeEnum type) {
        return switch (type) {
            case SINGLE_CHOICE_QUESTIONS -> new SingleChoiceQuestion();
            case MULTIPLE_CHOICE_QUESTIONS -> new MultipleChoiceQuestion();
            case TRUE_OR_FALSE_QUESTIONS -> new TrueOrFalseQuestion();
            case FILL_IN_THE_BLANK_QUESTIONS -> new FillInTheBlankQuestion();
            case SHORT_ANSWER_QUESTIONS -> new ShortAnswerQuestion();
            case MATERIAL_QUESTIONS -> new MaterialQuestion();
            case LINK_QUESTIONS -> new LinkQuestion();
        };
    }
}